# -*- coding: utf-8 -*-
"""
Project: automation-api-project
Creator: jinsheng
Create time: 2021-11-26 09:03
IDE: PyCharm
Introduction: ods_device_electric_ts表造数据
"""
from datetime import timedelta
from multiprocessing import Process
from threading import Thread
import random
from datetime import datetime
from common.hbaseDataOperate import HbaseOperate

lis = [i for i in range(0, 500000) if i % 5000 == 0]


def insert(start=None):
    hb = HbaseOperate(table_name='ods_device_electric_ts', version=156)
    fmt_s = '%Y%m%d%H%M%S'
    for j in range(start + 1, start + 5001):
        device_id = str(10000000 + j)
        # device_id = '121926203731877888'
        tm = datetime(2022, 1, 1, 0, 0)
        for i in range(96):
            tm_res = str(tm.strftime(fmt_s))[::-1]
            row_key = tm_res + device_id + 'mi15'
            data = {'cf:device_id': device_id, 'cf:data_time': tm_res,
                    'cf:efaa_actual_kwh': '1',
                    'cf:efab_actual_kwh': '1',
                    'cf:efac_actual_kwh': '1',
                    'cf:efat_actual_kwh': '1',
                    'cf:efrt_actual_kvarh': '1',
                    'cf:eraa_actual_kwh': '1',
                    'cf:erab_actual_kwh': '1',
                    'cf:erac_actual_kwh': '1',
                    'cf:erat_actual_kwh': '1',
                    'cf:errt_actual_kvarh': '1',
                    'cf:ds_origin_kw': '1',
                    'cf:dp_origin_kw': '1',
                    'cf:df_origin_kw': '1',
                    'cf:do_origin_kw': '1',
                    'cf:dt_origin_kw': '1',
                    'cf:indfaa_origin_kwh': '1',
                    'cf:indfab_origin_kwh': '1',
                    'cf:indfac_origin_kwh': '1',
                    'cf:indfat_origin_kwh': '1',
                    'cf:indfrt_origin_kvarh': '1',
                    'cf:indraa_origin_kwh': '1',
                    'cf:indrab_origin_kwh': '1',
                    'cf:indrac_origin_kwh': '1',
                    'cf:indrat_origin_kwh': '1',
                    'cf:indrp1_origin_kvarh': '1',
                    'cf:indrp2_origin_kvarh': '1',
                    'cf:indrp3_origin_kvarh': '1',
                    'cf:indrp4_origin_kvarh': '1',
                    'cf:indrrt_origin_kvarh': '1',
                    'cf:dt_time_empty': '1',
                    'cf:ds_time_empty': '1',
                    'cf:dp_time_empty': '1',
                    'cf:df_time_empty': '1',
                    'cf:do_time_empty': '1',
                    'cf:ua_origin_v': '1',
                    'cf:ub_origin_v': '1',
                    'cf:uc_origin_v': '1',
                    'cf:ia_origin_a': '1',
                    'cf:ib_origin_a': '1',
                    'cf:ic_origin_a': '1',
                    'cf:pa_actual_kw': '1',
                    'cf:pb_actual_kw': '1',
                    'cf:pc_actual_kw': '1',
                    'cf:pt_actual_kw': '1',
                    'cf:qa_actual_kvar': '1',
                    'cf:qb_actual_kvar': '1',
                    'cf:qc_actual_kvar': '1',
                    'cf:pfa_origin_empty': '1',
                    'cf:pfb_origin_empty': '1',
                    'cf:pfc_origin_empty': '1',
                    'cf:pft_origin_empty': '1',
                    'cf:qt_actual_kvar': '1',
                    'cf:iz_origin_a': '1',
                    'cf:uaa_origin_d': '1',
                    'cf:uab_origin_d': '1',
                    'cf:uac_origin_d': '1',
                    'cf:iaa_origin_d': '1',
                    'cf:iab_origin_d': '1',
                    'cf:iac_origin_d': '1',
                    'cf:ta_origin_dc': '1',
                    'cf:tb_origin_dc': '1',
                    'cf:tc_origin_dc': '1',
                    'cf:paa_origin_kw': '1',
                    'cf:pab_origin_kw': '1',
                    'cf:pac_origin_kw': '1',
                    'cf:iub_origin_d': '1',
                    'cf:uub_origin_d': '1',
                    'cf:data_source': '1', 'cf:frequency': 'mi15'}
            tm += timedelta(minutes=15)
            hb.insert_data(row_key, data)


def insert_one(start=None):
    hb = HbaseOperate(table_name='ods_device_electric_ts_1')
    fmt_s = '%Y%m%d%H%M%S'
    tm = datetime(2021, 11, 1, 0, 0)
    # device_id = str(100000000000000000 + start)
    device_id = '121926203731877888'
    indfat_origin_kwh = 2000.0
    indfrt_origin_kvarh = 500.0
    indrat_origin_kwh = 200.0
    indrrt_origin_kvarh = 50.0
    for i in range(192):
        tm_res = str(tm.strftime(fmt_s))[::-1]
        ua_origin_v = round(random.uniform(215.0, 225.0), 2)
        ub_origin_v = round(random.uniform(215.0, 225.0), 2)
        data_source = random.randint(1, 2)
        random_increase = round(random.uniform(10.0, 20.0), 2)
        row_key = tm_res + device_id + 'mi15'
        data = {'cf:device_id': device_id, 'cf:data_time': str(tm.strftime(fmt_s)),
                'cf:ua_origin_v': str(ua_origin_v), 'cf:ub_origin_v': str(ub_origin_v),
                'cf:indfat_origin_kwh': str(indfat_origin_kwh + random_increase),
                'cf:indfrt_origin_kvarh': str(indfrt_origin_kvarh + random_increase),
                'cf:indrat_origin_kwh': str(indrat_origin_kwh + random_increase),
                'cf:indrrt_origin_kvarh': str(indrrt_origin_kvarh + random_increase),
                'cf:data_source': str(data_source), 'cf:frequency': 'mi15'}
        tm += timedelta(minutes=15)
        hb.insert_data(row_key, data)
        indfat_origin_kwh += random_increase
        indfrt_origin_kvarh += random_increase
        indrat_origin_kwh += random_increase
        indrrt_origin_kvarh += random_increase


def ydd(start=None):
    hb = HbaseOperate(table_name='1_test_2', version=156)
    fmt_s = '%Y%m%d%H%M%S'
    for j in range(start + 1, start + 11):
        # for j in range(96):
        # device_id = str(100000000000000000 + j)
        device_id = str(10000000 + j)
        # device_id = '121926203731877888'
        tm = datetime(2019, 10, 1, 0, 0)
        for i in range(2880):
            tm_res = str(tm.strftime(fmt_s))[::-1]
            row_key = tm_res + device_id + 'mi15'
            data = {'cf:device_id': device_id, 'cf:data_time': tm_res,
                    'cf:a': '1',
                    'cf:b': '1',
                    'cf:c': '1',
                    'cf:d': '1',
                    'cf:data_source': '1', 'cf:frequency': 'mi15'}
            tm += timedelta(minutes=15)
            hb.insert_data(row_key, data)


if __name__ == '__main__':
    process_list = []
    for i in lis:  # 开启5个子进程执行fun1函数
        p = Process(target=insert, args=(i,))  # 实例化进程对象
        # p = Process(target=ydd, args=(i,))
        p.start()
        process_list.append(p)

    for i in process_list:
        i.join()

